//
//  RecommendedVehicleTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/6.
//

import UIKit
import SkeletonView
class RecommendedTableViewCell: UITableViewCell {
    var titleLab: UILabel!
    var arrowsBtn: UIButton!
    var collectionView: UICollectionView!
    var dataArray: [VehicleLisetModel] = []
    var contentModel: CarDetailsContentModel!
    var seaCarFlag: Bool = false //海外车标识
    var emptyView: LYEmptyView!
    var loadingFlag: Bool = false //是否加载过
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.backgroundColor = .white
        self.selectionStyle = .none
        self.clipsToBounds = true
        
        self.titleLab = UILabel(frame: .zero)
        self.titleLab.textColor = Theme.titleLabColor
        self.titleLab.font = UIFont(name: Theme.PingFangSCMedium, size: 15)
        self.titleLab.textAlignment = .center
        self.titleLab.text = MMLocalizedString("recommended_vehicle", comment: "推荐车辆")
        self.contentView.addSubview(self.titleLab)
        
        
        self.arrowsBtn = UIButton(frame: .zero)
        self.arrowsBtn.setTitle(MMLocalizedString("search_more", comment: "搜索更多"), for: .normal)
        self.arrowsBtn.setTitleColor(UIColor(hex: 0x333333), for: .normal)
        self.arrowsBtn.titleLabel?.font = UIFont(name: Theme.PingFangSCRegular, size: 12)
        self.arrowsBtn.semanticContentAttribute = .forceRightToLeft
        self.arrowsBtn.setImage(UIImage(named: "icon_news_arrows"), for: .normal)
        self.arrowsBtn.setImage(UIImage(named: "icon_news_arrows"), for: .highlighted)
        self.arrowsBtn.addTarget(self, action: #selector(arrowsBtnAntion), for: .touchUpInside)
        self.contentView.addSubview(self.arrowsBtn)
        
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 10
        layout.minimumLineSpacing = 10
        layout.scrollDirection = .vertical
        let ww = (kScreenWidth - 38)/2
        layout.itemSize = CGSize(width: ww, height: 210)
        layout.sectionInset = UIEdgeInsets(top: 0, left: 14, bottom: 0, right: 14)
        self.collectionView = UICollectionView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: 170), collectionViewLayout: layout)
        self.collectionView.delegate = self
        self.collectionView.dataSource = self
        self.collectionView.register(GoodsCollectionViewCell.self, forCellWithReuseIdentifier: "GoodsCollectionViewCell")
        self.collectionView.register(SkeletonCollectionCell.self, forCellWithReuseIdentifier: "SkeletonCollectionCell")
        self.collectionView.backgroundColor = .white
        self.contentView.addSubview(self.collectionView)
        self.collectionView.showsVerticalScrollIndicator = false
        self.collectionView.isScrollEnabled = false
        self.collectionView.isSkeletonable = true
        self.setUpNoData()
        self.addSnpLayout()
        self.collectionView.showAnimatedGradientSkeleton()
    }
    
    ///空视图
    func setUpNoData() {
        self.emptyView = LYEmptyView.emptyActionView(withImageStr: "icon_empty", titleStr: MMLocalizedString("no_more_data", comment: "暂时没有数据"), detailStr: "", btnTitleStr: "") {

        }
        self.emptyView.actionBtnBackGroundColor = Theme.viewBkgGrayColor
        emptyView.centerX = kScreenWidth/2
        emptyView?.subViewMargin = 28;
        emptyView?.contentViewOffset = -100
        emptyView?.isHidden = true
        self.collectionView.ly_emptyView = self.emptyView
    }
    
    
    private func addSnpLayout(){
        self.titleLab.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.left.equalTo(14)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        
        
        self.collectionView.snp.makeConstraints { make in
            make.top.equalTo(self.titleLab.snp.bottom).offset(13)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(kScreenHeight - NavBarHight)
            make.bottom.equalTo(-10)
        }
        
        self.arrowsBtn.snp.makeConstraints { make in
            make.top.equalTo(5)
            make.right.equalTo(-14)
            make.height.equalTo(20)
        }
        self.layoutIfNeeded()
    }
    
    ///setModel
    func setData(data: [VehicleLisetModel]){
        //表示请求过后台
        if self.loadingFlag {
            self.willShowSketchView()
        }
        self.dataArray = data
        if self.dataArray.count > 0 {
            self.collectionView.reloadData()
            self.collectionView.layoutSubviews()
            let hh = self.collectionView.collectionViewLayout.collectionViewContentSize.height
            self.collectionView.snp.updateConstraints { make in
                make.height.equalTo(hh)
            }
        }else{
            self.collectionView.reloadData()
            self.collectionView.snp.updateConstraints { make in
                make.height.equalTo(kScreenHeight - NavBarHight)
            }
        }
        self.updateConstraints()
    }
    
    private func willShowSketchView(){
        if self.dataArray.count > 0  {
            self.emptyView.isHidden = true
        }else{
            self.emptyView.isHidden = false
        }
        self.collectionView.hideSkeleton()
    }
    
    
    ///车辆详情
    func setDetailsModel(data: [VehicleLisetModel]){
        if self.loadingFlag {
            self.willShowSketchView()
        }
        self.titleLab.textAlignment = .left
        self.dataArray = data
        self.collectionView.reloadData()
        self.collectionView.layoutSubviews()
        let hh = self.collectionView.collectionViewLayout.collectionViewContentSize.height
        self.collectionView.snp.updateConstraints { make in
            make.height.equalTo(hh)
        }
        self.updateConstraints()
    }
    
    //MARK: - Antion
    @objc func arrowsBtnAntion(){
        if self.seaCarFlag {
            let vc = OverSeaListViewController()
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }else{
            let vc = UsedCarViewController()
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}


extension RecommendedTableViewCell: SkeletonCollectionViewDelegate, SkeletonCollectionViewDataSource {
    //skeletionView
    func collectionSkeletonView(_ skeletonView: UICollectionView, cellIdentifierForItemAt indexPath: IndexPath) -> SkeletonView.ReusableCellIdentifier {
        return "SkeletonCollectionCell"
    }
    
    ///SkeletonView 1组
    func numSections(in collectionSkeletonView: UICollectionView) -> Int {
        return 1
    }
    ///SkeletonView 20个item
    func collectionSkeletonView(_ skeletonView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 20
    }
    ///SkeletonView item
    func collectionSkeletonView(_ skeletonView: UICollectionView, skeletonCellForItemAt indexPath: IndexPath) -> UICollectionViewCell? {
        let cell = skeletonView.dequeueReusableCell(withReuseIdentifier: "SkeletonCollectionCell", for: indexPath) as! SkeletonCollectionCell
        cell.clipsToBounds = true
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.dataArray.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GoodsCollectionViewCell", for: indexPath) as! GoodsCollectionViewCell
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row]
            cell.setModel(model: model)
        }
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if self.dataArray.count > indexPath.row{
            let model = self.dataArray[indexPath.row]
            let vc = VehicleDetailsViewController()
            vc.id = "\(model.id ?? 0)"
            vc.seaCarFlag = self.seaCarFlag
            self.currentViewController()?.navigationController?.pushViewController(vc, animated: true)
        }
    }
    
    
}
